package com.sosolabs.com.site.client.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.sosolabs.com.site.client.presenter.CellTreePresenter;
import com.sosolabs.com.site.client.view.ContactTreeViewModel;


public class CellTreeView extends Composite implements CellTreePresenter.Display {

	@UiTemplate("ui/CellTreeView.ui.xml")
	interface Binder extends UiBinder<Widget, CellTreeView> {
	}

	private static Binder binder = GWT.create(Binder.class);

	@UiField
	Button confirmButton;
	
	@UiField
	TextBox name;
	
	@UiField
	TextBox address;
	
	@UiField
	CellTree cellTree;

	ListDataProvider<Contact> dataProvider;
	
	Contact cont ;
	
	@UiFactory CellTree makeCellTree() {
		
		 final SingleSelectionModel<Contact> selectionModel =
		      new SingleSelectionModel<Contact>();
		    selectionModel.addSelectionChangeHandler(
		        new SelectionChangeEvent.Handler() {
		          public void onSelectionChange(SelectionChangeEvent event) {
		            name.setText(selectionModel.getSelectedObject().name);
		            address.setText(selectionModel.getSelectedObject().address);
		          }
		        });

		    CellTree.Resources res = GWT.create(CellTree.BasicResources.class);
		    ContactTreeViewModel model = new ContactTreeViewModel(selectionModel);
		    cellTree = new CellTree(
		            new ContactTreeViewModel(selectionModel), null, res);
		    cellTree.setAnimationEnabled(true);

		return cellTree;
		
	}
	
	static interface Images extends ClientBundle {
	    ImageResource contact();
	  }
	
	 static class ContactCell extends AbstractCell<Contact> {

		    /**
		     * The html of the image used for contacts.
		     */
		    private final String imageHtml;

		    public ContactCell(ImageResource image) {
		      this.imageHtml = AbstractImagePrototype.create(image).getHTML();
		    }

		    @Override
		    public void render(Contact value, Object key, SafeHtmlBuilder sb) {
		      // Value can be null, so do a null check..
		      if (value == null) {
		        return;
		      }

		      sb.appendHtmlConstant("<table>");

		      // Add the contact image.
		      sb.appendHtmlConstant("<tr><td rowspan='3'>");
		      sb.appendHtmlConstant(imageHtml);
		      sb.appendHtmlConstant("</td>");

		      // Add the name and address.
		      sb.appendHtmlConstant("<td style='font-size:95%;'>");
		      sb.appendEscaped(value.name);
		      sb.appendHtmlConstant("</td></tr><tr><td>");
		      sb.appendEscaped(value.address);
		      sb.appendHtmlConstant("</td></tr></table>");
		    }
		  }
	
	public static class Contact {
	    String address;
	    private Date birthday;
	    public String name;
	    private static int nextid = 0;
	    private int id;

	    public Contact(String name, Date birthday, String address) {
	    	this.id = nextid;
	    	nextid++;
	      this.name = name;
	      this.birthday = birthday;
	      this.address = address;
	    }
	    
	    private int getId()
	    {
	    	return this.id;
	    }
	   
	  }

	  /**
	   * The list of data to display.
	   */
	  private static final List<Contact> CONTACTS = Arrays.asList(
			  new Contact("John", new Date(80, 4, 12), "123 Fourth Avenue"),
		      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln"),
		      new Contact("mary", new Date(85, 2, 22), "23 Lance Ln"),
		      new Contact("tom", new Date(85, 2, 22), "24 Lance Ln"),
		      new Contact("jany", new Date(85, 2, 22), "25 Lance Ln"),
		      new Contact("pony", new Date(85, 2, 22), "26 Lance Ln"),
		      new Contact("tom", new Date(85, 2, 22), "27 Lance Ln"),
		      new Contact("kate", new Date(85, 2, 22), "28 Lance Ln"),
		      new Contact("jany1", new Date(85, 2, 22), "25 Lance Ln1"),
		      new Contact("pony1", new Date(85, 2, 22), "26 Lance Ln1"),
		      new Contact("tom1", new Date(85, 2, 22), "27 Lance Ln1"),
		      new Contact("kate1", new Date(85, 2, 22), "28 Lance Ln1"),
		      new Contact("John2", new Date(80, 4, 12), "123 Fourth Avenue2"),
		      new Contact("Joe2", new Date(85, 2, 22), "22 Lance Ln2"),
		      new Contact("mary2", new Date(85, 2, 22), "23 Lance Ln2"),
		      new Contact("tom2", new Date(85, 2, 22), "24 Lance Ln2"),
		      new Contact("jany2", new Date(85, 2, 22), "25 Lance Ln2"),
		      new Contact("pony2", new Date(85, 2, 22), "26 Lance Ln2"),
		      new Contact("tom2", new Date(85, 2, 22), "27 Lance Ln2"),
		      new Contact("kate2", new Date(85, 2, 22), "28 Lance Ln2"),
		      new Contact("jany12", new Date(85, 2, 22), "25 Lance Ln12"),
		      new Contact("pony12", new Date(85, 2, 22), "26 Lance Ln12"),
		      new Contact("tom12", new Date(85, 2, 22), "27 Lance Ln12"),
		      new Contact("kate12", new Date(85, 2, 22), "28 Lance Ln12"),
		      new Contact("John3", new Date(80, 4, 12), "123 Fourth Avenue3"),
		      new Contact("Joe3", new Date(85, 2, 22), "22 Lance Ln3"),
		      new Contact("mary3", new Date(85, 2, 22), "23 Lance Ln3"),
		      new Contact("tom3", new Date(85, 2, 22), "24 Lance Ln3"),
		      new Contact("jany3", new Date(85, 2, 22), "25 Lance Ln3"),
		      new Contact("pony3", new Date(85, 2, 22), "26 Lance Ln3"),
		      new Contact("tom3", new Date(85, 2, 22), "27 Lance Ln3"),
		      new Contact("kate3", new Date(85, 2, 22), "28 Lance Ln3"),
		      new Contact("jany1", new Date(85, 2, 22), "25 Lance Ln13"),
		      new Contact("pony13", new Date(85, 2, 22), "26 Lance Ln13"),
		      new Contact("tom13", new Date(85, 2, 22), "27 Lance Ln13"),
		      new Contact("kate13", new Date(85, 2, 22), "28 Lance Ln13"),
		      new Contact("George", new Date(46, 6, 6), "1600 Pennsylvania Avenue"));
	  
	public CellTreeView() {
		initWidget(binder.createAndBindUi(this));
	}
	
	public HasClickHandlers getConfirmButton() {
		return confirmButton;
	}
	
	public Widget asWidget() {
		return this;
	}
}
